/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package svp;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import svp.exceptions.NonexistentEntityException;
import svp.exceptions.PreexistingEntityException;

/**
 *
 * @author 075087
 */
public class ProductUnitTblJpaController implements Serializable {

    public ProductUnitTblJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ProductUnitTbl productUnitTbl) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ProductColorTbl productColor = productUnitTbl.getProductColor();
            if (productColor != null) {
                productColor = em.getReference(productColor.getClass(), productColor.getProductColor());
                productUnitTbl.setProductColor(productColor);
            }
            em.persist(productUnitTbl);
            if (productColor != null) {
                productColor.getProductUnitTblCollection().add(productUnitTbl);
                productColor = em.merge(productColor);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findProductUnitTbl(productUnitTbl.getIproductunitID()) != null) {
                throw new PreexistingEntityException("ProductUnitTbl " + productUnitTbl + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ProductUnitTbl productUnitTbl) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ProductUnitTbl persistentProductUnitTbl = em.find(ProductUnitTbl.class, productUnitTbl.getIproductunitID());
            ProductColorTbl productColorOld = persistentProductUnitTbl.getProductColor();
            ProductColorTbl productColorNew = productUnitTbl.getProductColor();
            if (productColorNew != null) {
                productColorNew = em.getReference(productColorNew.getClass(), productColorNew.getProductColor());
                productUnitTbl.setProductColor(productColorNew);
            }
            productUnitTbl = em.merge(productUnitTbl);
            if (productColorOld != null && !productColorOld.equals(productColorNew)) {
                productColorOld.getProductUnitTblCollection().remove(productUnitTbl);
                productColorOld = em.merge(productColorOld);
            }
            if (productColorNew != null && !productColorNew.equals(productColorOld)) {
                productColorNew.getProductUnitTblCollection().add(productUnitTbl);
                productColorNew = em.merge(productColorNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = productUnitTbl.getIproductunitID();
                if (findProductUnitTbl(id) == null) {
                    throw new NonexistentEntityException("The productUnitTbl with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ProductUnitTbl productUnitTbl;
            try {
                productUnitTbl = em.getReference(ProductUnitTbl.class, id);
                productUnitTbl.getIproductunitID();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The productUnitTbl with id " + id + " no longer exists.", enfe);
            }
            ProductColorTbl productColor = productUnitTbl.getProductColor();
            if (productColor != null) {
                productColor.getProductUnitTblCollection().remove(productUnitTbl);
                productColor = em.merge(productColor);
            }
            em.remove(productUnitTbl);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ProductUnitTbl> findProductUnitTblEntities() {
        return findProductUnitTblEntities(true, -1, -1);
    }

    public List<ProductUnitTbl> findProductUnitTblEntities(int maxResults, int firstResult) {
        return findProductUnitTblEntities(false, maxResults, firstResult);
    }

    private List<ProductUnitTbl> findProductUnitTblEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ProductUnitTbl.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ProductUnitTbl findProductUnitTbl(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ProductUnitTbl.class, id);
        } finally {
            em.close();
        }
    }

    public int getProductUnitTblCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ProductUnitTbl> rt = cq.from(ProductUnitTbl.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
